// miniprogram/pages/q1/q1.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 2,
     radio: '1',
     pageSex:[
        {
        name:"男"
        },
       {
         name: "女"
       }
     ]
  },
  onClickIcon(){
    wx.showToast({
      title: '真实姓名只能输入一次无法修改!',
      icon:"none"
    })
  },
  onChange(event) {
    this.setData({
      radio: event.detail
    });
  },
  exitThis() {
    wx.navigateBack({})
  },
  upUser() {
    var utils = require('../../utils/getTool');
    var util = require('../../utils/promisify');
    var request = util(wx.request);
    if (this.data.userInfo.sex === "男" || this.data.userInfo.sex === "女") {
      wx.showLoading({
        title: '请等待',
      })
      console.log(this.data)
      request({
        url: getApp().appData.site+"/accp/psUser/upUser",
        data: {
          userName: this.data.userInfo.userName,
          nickName: this.data.userInfo.nickName,
          sex: this.data.userInfo.sex,
          age: this.data.userInfo.age,
          email: this.data.userInfo.email,
          phoneTel: this.data.userInfo.phoneTel,
          avatarUrl: this.data.userInfo.avatarUrl,
          openid: this.data.userInfo.openid
        }
      }).then(res => {
        wx.hideLoading();
        if (res.statusCode===200) {
          wx.showToast({
            title: '修改成功!',
            icon:"none"
          })
          getApp().getInfo(getApp().appData.site+"/accp/psUser/fetchUser").then(info => {
              //如果去数据库读取成功 那么添加到本地缓存 拿数据通过 wx.setStorageSync('userInfo', info.data.user);
            if (info.data.user) {
              wx.setStorageSync('userInfo', info.data.user);
              setTimeout(this.like, 1500) //1秒后跳转到首页
            }
          })
          .catch(err => { console.log(err) })
        } else {
          wx.showToast({
            title: '该用户已存在!',
            icon: "none"
          })
        }
      }).catch(err => {
        console.log(err);
      })
    } else {
      wx.showToast({
        title: '请输入正确性别',
        icon: "none"
      })
    }
  },
  like() {
    wx.switchTab({
      url: '../indexs/indexs',
    })
  },
  check: function(opotions) {
    wx.navigateTo({
      url: '../check/check',
    })
  },
  chuli: function(opotions) {
    wx.navigateTo({
      url: '../chuli/chuli',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //拿本地缓存数据
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo) { 
      this.setData({
        userInfo: userInfo
      }) //拿代办列表数据
      wx.request({
        url: getApp().appData.site +'/accp/psUser/job',
        data: {
          id: userInfo.id
        }, success: (res) => {
            console.log(res) //暂时没做
          if (res.statusCode===200){
            this.setData({
              ipsInDetail: res.data.ipsInDetail,
              ipsSoDetail: res.data.ipsSoDetail
            })
          }
        }, fail: err => { console.log(err) }
      })
    } else {
      wx.getUserInfo({ //因为有些数据特殊点所以要再次获取
        success: (res) => {
          this.data.userInfo = res.userInfo
          this.data.userInfo.openid = wx.getStorageSync('buf_openId')
        }
      })
    }
  },
  /** 
   * 以下都是绑定对应的输入框
   */
  onUserName: function(e) {
    this.data.userInfo.userName = e.detail;
  },
  onnickName: function(e) {
    this.data.userInfo.nickName = e.detail;
  },
  onSex: function(e) {
    this.setData({
      showSex:true
    })
  },
  seleSex(e){
    this.data.userInfo.sex = e.detail.name
    this.setData({
      showSex: false,
      userInfo: this.data.userInfo
    })
  },
  hideSex(){
    this.setData({
      showSex: false
    })
  },
  onAge: function(e) {
    this.data.userInfo.age = e.detail;
  },
  onPhoneTel(e) {
    this.data.userInfo.phoneTel = e.detail;
  },
  onEmail: function(e) {
    this.data.userInfo.email = e.detail;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})